/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lucene.simple.spellCheck;

//import java.io.File;
//import org.apache.lucene.search.spell.PlainTextDictionary;
//import org.apache.lucene.search.spell.SpellChecker;
//import org.apache.lucene.store.Directory;
//import org.apache.lucene.store.FSDirectory;

/**
 *
 * @author Content Pilot
 */
public class SimpleSpellChecker {
    
    java.io.File dir;
    String wordForSuggestion;
    int suggestionsNumber;
    
    public SimpleSpellChecker(String dirStr, String wordForSuggestion, int suggestionsNumber) {
        this.dir = new java.io.File(dirStr);  // where generate index files
        this.wordForSuggestion = wordForSuggestion;
        this.suggestionsNumber = suggestionsNumber;
    }
    
    public void didYouMean() throws Exception {
        org.apache.lucene.store.Directory directory = org.apache.lucene.store.FSDirectory.open(dir); //a flat list of files only for read
        
        org.apache.lucene.search.spell.SpellChecker spellChecker = new org.apache.lucene.search.spell.SpellChecker(directory);
        spellChecker.indexDictionary(
                new org.apache.lucene.search.spell.PlainTextDictionary(new java.io.File("c:/spellchecker/dict.txt")));
        String[] suggestions = spellChecker.suggestSimilar(wordForSuggestion, suggestionsNumber);
        if (suggestions != null && suggestions.length > 0) {
            for (String word : suggestions) {
                System.out.println("Did you mean: " + word);
            }
        } else {
            System.out.println(" No suggestions found for word: " + wordForSuggestion);
        }  
        
        directory.close();
    }
}
